<?php

defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);

class buy_list extends admin {

    public function __construct() {
        parent::__construct();
    }

    const str1 = '您购买的 "{name}" 已经发货, 请注意查收';
    const str2 = '您购买的 "{name}" 已被取消';

    public function init() {
        $db_mall = pc_base::load_model('mall_model');

        if ($_GET['status1'] == 0 && $_GET['status2']) {
            $id = (int)$_GET['id'];
            $status2 = (int)$_GET['status2'];

            $db_reminds = pc_base::load_model('reminds_model');
            $db_shop = pc_base::load_model('shop_model');

            $r = $db_mall->update(['status' => $status2], ['id' => $id, 'status' => 0]);
            if ($r) {
                $r = $db_mall->get_one(['id' => $id]);
                $uid = $r['uid'];
                $shop_id = $r['a_id'];
                $shop_res = $db_shop->pkhs($shop_id);

                if ($status2 == 1) {
                    $str = self::str1;
                } else {
                    $str = self::str2;
                }

                $str = str_replace('{name}', $shop_res['title'], $str);

                $info = [
                    'uid' => $uid,
                    'type' => 4,
                    'title' => $str,
                    'small_content' => $str,
                    'ymd' => date('Y-m-d'),
                    'ymdhis' => date('Y-m-d H:i:s'),
                ];

                $db_reminds->insert($info);

                showmessage('操作成功', '/?m=content&c=buy_list&a=init');
            } else {
                showmessage('操作失败', '/?m=content&c=buy_list&a=init');
            }
        }

        $where = '1';
        $status = isset($_GET['status']) ? (int)$_GET['status'] : -1;
        if (in_array($status, [0, 1, 2])) {
            $where .= " and `status`='$status'";
        }

        $uid = $_GET['uid'] ? (int)$_GET['uid'] : '';
        if ($uid) {
            $where .= " and uid=$uid";
        }

        $a_id = $_GET['a_id'] ? (int)$_GET['a_id'] : '';
        if ($a_id) {
            $where .= " and a_id=$a_id";
        }

        $datas = $db_mall->listinfo($where, 'id desc', $_GET['page']);
        $pages = $db_mall->pages;

        $g_url = get_url();

        include $this->admin_tpl('buy_list');
    }
}

function status2cn($a) {
    switch ($a) {
        case 0:
            return '待发货';
            break;
        case 1:
            return '已发货';
            break;
        case 2:
            return '已取消';
            break;
    }
}